package edu.jiefengli.course_selection_system.controller;

import edu.jiefengli.course_selection_system.pojo.entity.Course;
import edu.jiefengli.course_selection_system.pojo.entity.CourseRecord;
import edu.jiefengli.course_selection_system.pojo.entity.Result;
import edu.jiefengli.course_selection_system.pojo.entity.User;
import edu.jiefengli.course_selection_system.pojo.vo.SelectRecord;
import edu.jiefengli.course_selection_system.service.CourseRecordService;
import edu.jiefengli.course_selection_system.service.CourseService;
import edu.jiefengli.course_selection_system.service.UserService;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseRecordService courseRecordService;
    @Autowired
    private UserService userService;

//    @GetMapping("/course")
//    public Result getCourse(){
//        return Result.success(courseService.getAllCourses());
//    }

    @GetMapping("/record")
    public Result getCourseRecord(Integer id){
        List<SelectRecord> selectRecord = new ArrayList<>();
        List<CourseRecord> courseRecords = courseRecordService.getCourseRecordsById(id);
        for (CourseRecord courseRecord : courseRecords) {
            SelectRecord record = new SelectRecord();
            Course course = courseService.getCourseById(courseRecord.getCourseId());
            record.setCourse(course);
            record.setCourseRecord(courseRecord);
            User teacher = userService.getUserById(course.getTeacherId());
            record.setTeacher(teacher);
            selectRecord.add(record);
        }
        return Result.success(selectRecord);
    }

    @PostMapping("/record")
    public Result selectCouse(Integer studentId, Integer courseId){
        if (courseRecordService.addRecord(studentId, courseId)) {
            return Result.success("选课成功");
        }
        else {
            return Result.success("已选过该课");
        }
    }

    @DeleteMapping("/record")
    public Result pullbakSelect(int studentId, int courseId){
        courseRecordService.deleteRecord(studentId, courseId);
        return Result.success();
    }
}
